<template>
  <div class="edit-profile-container">
    <header class="page-header">
      <el-button icon="ArrowLeft" @click="$router.back()">返回</el-button>
      <h2>编辑个人信息</h2>
    </header>
  <div class="profile-card">
    <el-form :model="form" label-width="80px" @submit.prevent="submit">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" show-password />
      </el-form-item>

      <el-form-item label="性别">
        <el-select v-model="form.gender" placeholder="请选择性别">
          <el-option label="女" :value="0" />
          <el-option label="男" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>

      <el-form-item label="头像">
    <el-upload
    action="/api/uploadAvatar"
    list-type="picture-card"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
    :show-file-list="false"
    :headers="uploadHeaders"
    class="avatar-uploader">
    <img v-if="form.image" :src="form.image" class="avatar" />
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>
</el-form-item>

      <el-form-item>
  <div class="button-group">
    <el-button type="primary" native-type="submit" class="save-button">保存</el-button>
    <el-button @click="$router.back()" class="cancel-button">取消</el-button>
  </div>
</el-form-item>
    </el-form>
  </div>  
  </div>
</template>

<script setup>
import { ref, onMounted,computed } from 'vue'
import { useRouter } from 'vue-router'
import { getMeInfo as getMe, editMe as updateUserInfo } from '@/api/me'
import { ElMessage } from 'element-plus'

const router = useRouter()
const form = ref({
  username: '',
  password: '',
  gender: null,
  phone: '',
  image: ''
})
const uploadHeaders = computed(() => {
  const token = localStorage.getItem('authToken')
  return {
    Authorization: `Bearer ${token}`
  }
})
// 获取当前用户信息
onMounted(async () => {
  try {
    const res = await getMe()
    if (res.code === 1) {
      form.value = {
        username: res.data.username || '',
        password: res.data.password|| '', 
        gender: res.data.gender,
        phone: res.data.phone || '',
        image: res.data.image || 'https://wacoffee.oss-cn-beijing.aliyuncs.com/avatar/1c7fb35d-e887-4476-b7a8-294bc947a176.jpg'
      }
    } else {
      ElMessage.error(res.msg || '获取用户信息失败')
    }
  } catch (error) {
    ElMessage.error('网络错误，请重试')
  }
})

// 提交修改
const submit = async () => {
  try {
    const res = await updateUserInfo(form.value)
    if (res.code === 1) {
      ElMessage.success('修改成功')
      router.push('/me')
    } else {
      ElMessage.error(res.msg || '修改失败')
    }
  } catch (error) {
    ElMessage.error('更新失败：' + error.message)
  }
}

// 头像上传成功回调
const handleAvatarSuccess = (response, file) => {
  if (response.code === 1) {
    // 假设后端返回的是 { code: 1, data: 'https://oss.example.com/avatar.png' }
    form.value.image = response.data
    ElMessage.success('头像上传成功')
  } else {
    ElMessage.error(response.msg || '头像上传失败')
  }
}

// 头像上传前校验
const beforeAvatarUpload = (rawFile) => {
  const isValidType = ['image/jpeg', 'image/png'].includes(rawFile.type)
  const isValidSize = rawFile.size / 1024 / 1024 < 2

  if (!isValidType) {
    ElMessage.error('只能上传 JPG/PNG 格式图片')
    return false
  }

  if (!isValidSize) {
    ElMessage.error('图片大小不能超过 2MB')
    return false
  }

  return true
}
</script>

<style scoped>
.el-form-item {
  position: relative;
  margin-bottom: 24px;
}


.el-form-item::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -6px;
  width: 100%;
  height: 1px;
  background: linear-gradient(to right, transparent, #ccc, transparent);
  transition: opacity 0.3s ease;
}

.el-form-item:hover::after {
  opacity: 0.6;
}
.page-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: #fff;
  margin-bottom: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  border-radius: 12px;
}

.page-header h2 {
  margin: 0;
  font-size: 20px;
  color: #333;
  margin-left: 10px;
}

.el-form-item__content {
  transition: all 0.3s ease;
}

.el-input,
.el-select {
  width: 100%;
}

.el-input__inner {
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 14px;
  transition: border-color 0.3s ease;
}

.el-input__inner:focus {
  border-color: #5c6bc0;
  box-shadow: 0 0 0 2px rgba(92, 107, 192, 0.2);
}
.button-group {
  display: flex;
  gap: 100px;
  justify-content: center;
  margin-top: 20px;
}

.save-button {
  background: linear-gradient(135deg, #5c6bc0, #7e57c2);
  border: none;
  color: white;
  font-weight: bold;
  border-radius: 24px;
  padding: 12px 30px;
  transition: all 0.3s ease;
}

.save-button:hover {
  background: linear-gradient(135deg, #7e57c2, #5c6bc0);
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(92, 107, 192, 0.3);
}

.cancel-button {
  border: 1px solid #ddd;
  color: #666;
  border-radius: 24px;
  padding: 12px 30px;
  background-color: #f8f9fa;
  transition: all 0.3s ease;
}

.cancel-button:hover {
  background-color: #e9ecef;
}
.avatar-uploader {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
}

.avatar-uploader:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.avatar-uploader-icon {
  font-size: 24px;
  color: #8c9eff;
  line-height: 100px;
  text-align: center;
}

.avatar-tip {
  font-size: 12px;
  color: #999;
  margin-top: 8px;
}
/* 表单容器卡片 */
.profile-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  padding: 24px;
  max-width: 600px;
  margin: 0 auto;
}
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.edit-profile-container {
  padding: 30px;
  background-color: #f8f9fa;
  min-height: 100vh;
}


.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}
@media (max-width: 768px) {
  .button-group {
    flex-direction: column;
    gap: 16px;
    width: 100%;
  }

  .save-button,
  .cancel-button {
    width: 100%;
    font-size: 16px;
    padding: 12px 0;
  }

  .profile-card {
    padding: 16px;
  }

  .el-form-item::after {
    display: none;
  }

  .page-header {
    padding: 12px 16px;
  }

  .page-header h2 {
    font-size: 18px;
  }
}
</style>